package com.ethink.platform.manager;

import com.alibaba.fastjson.JSONObject;
import com.ethink.mdm.api.component.MdmComponent;
import com.ethink.order.api.PurchaseOrderFacade;
import com.ethink.order.api.enums.PurchaseOrderStatusEnum;
import com.ethink.order.api.info.PurchaseOrderDetailInfo;
import com.ethink.order.api.info.PurchaseOrderInfo;
import com.ethink.order.api.request.PurchaseOrderUpdateStatusRequest;
import com.ethink.order.api.response.OverseasDeleveryPreQueryResponse;
import com.ethink.platform.common.properties.PlatformCommonProperties;
import com.ethink.system.api.SystemTodoFacade;
import com.ethink.system.api.enums.SystemTodoTodoTypeEnum;
import com.ethink.system.api.request.SystemTodoAddRequest;
import com.ethink.warehouse.api.LogisticsTraceApplyOrderFacade;
import com.ethink.warehouse.api.enums.InventoryTypeEnum;
import com.ethink.warehouse.api.enums.LogisticsTraceDetailOrderTypeEnum;
import com.ethink.warehouse.api.enums.SubjectEnum;
import com.ethink.warehouse.api.request.LogisticsTraceApplyAddRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author default
 * @version 1.0.0
 * @ClassName PurchaseManager.java
 * @Description
 * @createTime 2022年11月11日 16:43:00
 */
@Component
@Slf4j
public class PurchaseManager {

    @Autowired
    private PurchaseOrderFacade purchaseOrderFacade;
    @Autowired
    private MdmComponent mdmComponent;

    @Autowired
    private LogisticsTraceApplyOrderFacade logisticsTraceApplyOrderFacade;

    @Autowired
    private PlatformCommonProperties commonProperties;

    @Autowired
    private SystemTodoFacade systemTodoFacade;


    public void overseasDelivery(Long id) {
        OverseasDeleveryPreQueryResponse response = purchaseOrderFacade.overseasDeliveryPreQuery(id);
        t11(response.getPurchaseOrderInfo(), response.getPurchaseOrderDetailInfos());
        PurchaseOrderUpdateStatusRequest updateStatusRequest = new PurchaseOrderUpdateStatusRequest();
        updateStatusRequest.setId(id);
        updateStatusRequest.setOrderStatus(PurchaseOrderStatusEnum.S3.getIntValue());
        purchaseOrderFacade.updateStatus(updateStatusRequest);
        //创建代办事项
        try {
            SystemTodoAddRequest addRequest = new SystemTodoAddRequest();
            addRequest.setRoleName(commonProperties.getLogisticsRoleName());
            addRequest.setTodoType(SystemTodoTodoTypeEnum.T5.getIntValue());
            addRequest.setDataId(response.getPurchaseOrderInfo().getOrderNo());
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("orderNo", response.getPurchaseOrderInfo().getOrderNo());
            addRequest.setTodoMessage(jsonObject.toJSONString());
            systemTodoFacade.addSystemTodo(addRequest);
        } catch (Exception e) {
            log.error("生成待办失败", e);
        }
    }

    /**
     * 采购入库申请创建
     */
    private void t11(PurchaseOrderInfo purchaseOrderInfo, List<PurchaseOrderDetailInfo> detailInfoList) {
        LogisticsTraceApplyAddRequest addRequest = new LogisticsTraceApplyAddRequest();
        addRequest.setOrderType(LogisticsTraceDetailOrderTypeEnum.T1.getIntValue());
        addRequest.setSourceOrderNo(purchaseOrderInfo.getOrderNo());
        addRequest.setSourceOrderType(SubjectEnum.T11.getCode());
        addRequest.setSubject(SubjectEnum.T11.getCode());
        addRequest.setCreateUserCode(purchaseOrderInfo.getCreateUserCode());
        addRequest.setCreateUserName(purchaseOrderInfo.getCreateUserName());
        addRequest.setCompanyCode(purchaseOrderInfo.getSupplierCode());
        addRequest.setCompanyName(purchaseOrderInfo.getSupplierName());
        addRequest.setLock(false);

        addRequest.setOrderType(LogisticsTraceDetailOrderTypeEnum.T1.getIntValue());
        addRequest.setSourceOrderNo(purchaseOrderInfo.getOrderNo());
        addRequest.setCreateUserCode(purchaseOrderInfo.getCreateUserCode());
        addRequest.setCreateUserName(purchaseOrderInfo.getCreateUserName());
        addRequest.setT11Items(detailInfoList.stream().map(detailInfo -> {
            LogisticsTraceApplyAddRequest.T11Item item = new LogisticsTraceApplyAddRequest.T11Item();
            item.setSourceDetailOrderNo(detailInfo.getDetailOrderNo());
            item.setMateSku(detailInfo.getMateSku());
            item.setInventoryType(InventoryTypeEnum.ZP.getIntValue());
            item.setApplyInQty(detailInfo.getLockOrderQty());
            item.setSubWarehouseCode(purchaseOrderInfo.getSubWarehouseCode());
            if(StringUtils.isBlank(purchaseOrderInfo.getSaleSubChannel())) {
                item.setSaleSubChannel("Z000");
            } else {
                item.setSaleSubChannel(purchaseOrderInfo.getSaleSubChannel());
            }
            // 采购入库 提供采购信息
            item.setPurchaseOrderNo(detailInfo.getOrderNo());
            item.setPurchaseDetailOrderNo(detailInfo.getDetailOrderNo());
            item.setCostPrice(detailInfo.getPurchasePriceBb());
            return item;
        }).collect(Collectors.toList()));
        logisticsTraceApplyOrderFacade.addLogisticsTraceApplyOrder(addRequest);
    }


}
